package net.ib.mtalk.network.task;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import net.ib.mntalk.protocol.Common;
import net.ib.mntalk.protocol.Common.MnTalkAccountInfo;
import net.ib.mntalk.protocol.Common.UUIDMsg;
import net.ib.mntalk.protocol.Session.GetSuggestionsRes;
import net.ib.mntalk.protocol.Session.GetSuggestionsVersionReq;
import net.ib.mntalk.protocol.Session.GetSuggestionsVersionRes;
import net.ib.mtalk.constant.HandlerMsgConst;
import net.ib.mtalk.database.helper.SuggestionDatabaseHelper;
import net.ib.mtalk.global.GlobalPreferences;
import net.ib.mtalk.network.MethodId.Session;
import net.ib.mtalk.network.TcpEnvelope;
import net.ib.mtalk.util.GpbSupport;
import net.ib.mtalk.util.MTalkLogWriter;
import net.ib.mtalk.util.NotifyingAsyncQueryHandler;

public class GetSuggestionsVersionTask extends AsyncNetTask {
	
	private GetSuggestionsVersionReq mGetSuggestionsVersionReq;

	public GetSuggestionsVersionTask(Context _context, Handler _uiHandler, GetSuggestionsVersionReq getSuggestionsVersionReq) {
		super(_context, _uiHandler);
		mContext = _context;
		mUiHandler = _uiHandler;
		this.mGetSuggestionsVersionReq = getSuggestionsVersionReq;
	}

	@Override
	protected TcpEnvelope onPreExecute() {
		TcpEnvelope rev = new TcpEnvelope.Builder()
			.setType(TcpEnvelope.REQUEST)
			.setMessage(mGetSuggestionsVersionReq)
			.setMethodId(Session.Suggestion.GetSuggestionsVersion)
			.build();
		return rev;
	}

	@Override
	protected void onPostExecute(int resultCode, Object sentObject, Object receivedObject) {
		MTalkLogWriter.d("GetSuggestionsVersionTask.onPostExecute() resultCode : " + resultCode);
		if (receivedObject == null) {
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_UNKNOWN_ERROR));
			return;
		}
		
		TcpEnvelope resultEnvelope = (TcpEnvelope) receivedObject;
		GetSuggestionsVersionRes getSuggestionsVersionRes = (GetSuggestionsVersionRes) resultEnvelope.getMessage();
		
		MTalkLogWriter.d("GetSuggestionsVersionTask ResponseCode : " + getSuggestionsVersionRes.getResponseCode());
		
		if(getSuggestionsVersionRes.getResponseCode() == Common.SUCCESS){
			Message msg = mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_GET_SUGGESTION_VERSION_SUCCESS);
			msg.obj = getSuggestionsVersionRes.getVersion();
			mUiHandler.sendMessageAtFrontOfQueue(msg);
		} else {
			mUiHandler.sendMessageAtFrontOfQueue(mUiHandler.obtainMessage(HandlerMsgConst.HANDLER_GET_SUGGESTION_VERSION_FAIL));
		}
	}

}
